import Pages from '../../pages/pages.vue'
import productNew from '../product-new/product-new.vue'
import classifyNew from '../classify-new/classify-new.vue'

import { setMenuType, getSectionList, deleteSectionItem, deleteClassifyIdItem, swapSectionItem, swapClassfySwap, showMsg } from '../../../vuex/action'

export default {
  name: 'product-list',
  components: {
    'vue-pages': Pages,
    'product-new': productNew,
    'classify-new': classifyNew
  },
  data() {
    return {
      showNew: false,
      showClassifyNew: false,
      title: '',
      classifyTitle: '',
      it: {},
      classifyIt: {},
      cList: [],
    }
  },
  vuex: {
    getters: {
      fresh: state => state.inviteListFresh,
    },
    actions: {
      xSetMenuType: setMenuType,
      xGetSectionList: getSectionList,
      xDeleteSectionItem: deleteSectionItem,
      xDeleteClassifyIdItem: deleteClassifyIdItem,
      xSwap: swapSectionItem,
      xClassfySwap: swapClassfySwap,
      xShowMsg: showMsg
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList({})
  },
  methods: {
    getList() {
      this.xGetSectionList()
        .then((data) => {
          this.cList = data.list
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    toChild(classifyId) {
      this.$route.router.go('/child')
      this.$route.router.go('/child/' + classifyId)
    },
    newProduct(it, title) {
      this.showNew = true
      this.it = it
      this.title = title
    },
    newClassify(sectionId, it, title) {
      this.showClassifyNew = true
      this.classifyIt = it
      this.classifyIt.sectionId = sectionId
      this.classifyTitle = title
    },
    deleteSection(sectionId) {
      this.xDeleteSectionItem({
        sectionId: sectionId
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    deleteClassify(classifyId) {
      this.xDeleteClassifyIdItem({
        classifyId: classifyId
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    swap(id1, id2) {
      this.xSwap({
        fromId: id1,
        toId: id2
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    classfySwap(id1, id2) {
      this.xClassfySwap({
        fromId: id1,
        toId: id2
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
